using System;
using System.Collections.Generic;
using System.Text;
using Ltf.Remoting.Settings;
using System.Net;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.IO;

namespace Ltf.Remoting.TeamFoundation
{
	public class TFServerConnection
	{
		private TFSettings tfSettings;

		public TFServerConnection(TFSettings settings)
		{
			this.tfSettings = settings;
			if (tfSettings.User != string.Empty)
			{
				NetworkCredential nc;
				if (!string.IsNullOrEmpty( tfSettings.Domain))
					nc = new NetworkCredential(tfSettings.User, tfSettings.Password, tfSettings.Domain);
				else
					nc = new NetworkCredential(tfSettings.User, tfSettings.Password);

				if (tfSettings.Server != null)
					teamFoundationServer = new TeamFoundationServer(tfSettings.Server, nc);
				else
					return;
			}
			else
			{
				teamFoundationServer = TeamFoundationServerFactory.GetServer(tfSettings.Server);
			}
			versionControlServer = (VersionControlServer)teamFoundationServer.GetService(typeof(VersionControlServer));
		}

		private TeamFoundationServer teamFoundationServer;
		public TeamFoundationServer TeamFoundationServer
		{
			get { return teamFoundationServer; }
		}


		private VersionControlServer versionControlServer;
		public VersionControlServer VersionControlServer
		{
			get { return versionControlServer; }
		}

		public Workspace GetWorkspace(string path)
		{
			Workstation ws = Workstation.Current;
			WorkspaceInfo wsi = Workstation.Current.GetLocalWorkspaceInfo(path);
			if (wsi != null && path != string.Empty)
				return wsi.GetWorkspace(teamFoundationServer);
			else
				return null;
	//			throw new Ltf.Remoting.Exceptions.LizardException(string.Format("Could not create Workspace for path:'{0}'",path));
		}

		public WorkingFolder GetWorkingFolder(string path)
		{
			return GetWorkspace(path).GetWorkingFolderForLocalItem(path);
		}

		public TFSettings Settings
		{
			get { return tfSettings; }
		}


		public string GetMetaFolder(string folderPath)
		{
			return GetMetaFolder(folderPath, false);
		}

		public string GetMetaFolder(string folderPath, bool createIfNotExisting)
		{
			if (folderPath.EndsWith("\\"))
				folderPath = folderPath.Substring(0, folderPath.Length - 1);
			if (File.Exists(folderPath))
				folderPath = Path.GetDirectoryName(folderPath);
			// make sure drive letter is upper case!
			folderPath = folderPath.Substring(0, 1).ToUpper() + folderPath.Substring(1);
			System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
			byte[] bytes = new byte[folderPath.Length];
 			string metaFolderName = Convert.ToBase64String( md5.ComputeHash(System.Text.Encoding.ASCII.GetBytes(folderPath))).Replace('/','-');
			
			//split it into 3 to keep the number of folders per folder to a reasonable level.

			string part1 = metaFolderName.Substring(0, 1);
			string part2 = metaFolderName.Substring(1, 1);
			string part3 = metaFolderName.Substring(2);

			string metaFolderPath=Path.Combine(Settings.LtfMetaFolderName, part1);

			if (!Directory.Exists(metaFolderPath) && createIfNotExisting)
				Directory.CreateDirectory(metaFolderPath);

			 metaFolderPath = Path.Combine(metaFolderPath, part2);

			if (!Directory.Exists(metaFolderPath) && createIfNotExisting)
				Directory.CreateDirectory(metaFolderPath);

			 metaFolderPath = Path.Combine(metaFolderPath, part3);

			if (!Directory.Exists(metaFolderPath) && createIfNotExisting)
				Directory.CreateDirectory(metaFolderPath);

			return metaFolderPath;
		}
	}
}
